<template>
    <el-dialog
            :center="true"
            :visible="show"
            @close="close"
            :show-close="true"
            lock-scroll
            title="资讯发布"
    >
        <el-form label-width="40px">
            <el-form-item class="marginBottom" label="标题">
                <el-input class="borderNone" clearable placeholder="请输入标题" v-model="sub.title"></el-input>
            </el-form-item>
            <el-form-item class="marginBottom" label="内容">
                <el-input
                        class="borderNone"
                        type="textarea"
                        :autosize="{ minRows: 3}"
                        placeholder="请输入内容"
                        clearable
                        v-model="sub.content"
                ></el-input>
            </el-form-item>
            <el-form-item class="marginBottom" label="类型">
                <el-select v-model="sub.messageTypeId" placeholder="请选择">
                    <el-option v-for="item in messageType" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="marginBottom">
                <el-button @click="submit" type="primary">发布</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>
</template>

<script>
  export default {
    name: "MessageAdd",
    props : {
      show : {
        type : Boolean,
        default : false,
      },
      userId : {
        type : String,
      }
    },
    data(){
      return {
        sub : {
          title: "",
          content: "",
          messageTypeId: undefined,
          userId : this.userId,
          createDate : new Date().toLocaleDateString(),
          flag : 0, // 默认待审核
        },
        messageType : [],
      }
    },
    created() {
      this.$api.message.getMessageType().then(res => {
        this.messageType = res.data;
      })
    },
    methods : {
      submit(){
        console.log(this.sub);
        this.$api.message.messageAdd(this.sub)
            .then(() => {
              this.$message({
                message : "发布成功,请等待审核通过!",
                type : "success",
              });
              this.close();
            })
            .catch(error => {
              this.$message({
                message : error,
                type : "error",
              });
            })
      },
      close(){
        this.$emit("close");
      }
    }
  }
</script>

<style scoped>

</style>
